﻿
using Abp.Application.Services.Dto;
using Abp.Web.Models;
using Abp.Web.Mvc.Authorization;
using KDS.Mes.Mes.TestImss;
using KDS.Mes.Mes.TestImss.Dtos;
using KDS.Mes.Web.Models.TestImss;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace KDS.Mes.Web.Controllers
{
    public class TestImssController : MesControllerBase
    {

        private readonly ITestImsAppService _testImsAppService;

        public TestImssController(ITestImsAppService testImsAppService)
        {
            _testImsAppService = testImsAppService;

        }

        public ActionResult Index()
        {
            var lines = new List<ComboboxItemDto>();
            lines.Insert(0, new ComboboxItemDto("", ""));
            lines.Insert(1, new ComboboxItemDto("X001", "一线"));
            lines.Insert(2, new ComboboxItemDto("X002", "二线"));
            lines.Insert(3, new ComboboxItemDto("X003", "三线"));

            var model = new TestImssViewModel
            {
                FilterText = Request.QueryString["filterText"],
                Lines = lines
            };
            return View(model);
        }

        [DontWrapResult]
        public JsonResult GetAllTestImss(int limit, int offset, string sortfiled, string sortway,
                                     string lineId, string seatID, DateTime? time1, DateTime? time2)
        {
            var sort = !string.IsNullOrEmpty(sortfiled) ? string.Format("{0} {1}", sortfiled, sortway) : "";
            var filter = new GetTestImsInput
            {
                SkipCount = offset,
                MaxResultCount = limit,
                Sorting = sort,
                LineId = lineId,
                SeatID = seatID,
                Time1 = time1,
                Time2 = time2,
            };
            var pagedTasks = _testImsAppService.GetPagedTestImss(filter);
            return Json(new { total = pagedTasks.TotalCount, rows = pagedTasks.Items }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 根据id获取进行编辑或者添加的用户信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        //[AbpMvcAuthorize(TestImsAppPermissions.TestIms_CreateTestIms,TestImsAppPermissions.TestIms_EditTestIms)]
        public async Task<PartialViewResult> CreateOrEditTestImsModal(int? id)
        {
            var input = new NullableIdDto<int> { Id = id };

            var output = await _testImsAppService.GetTestImsForEditAsync(input);

            var viewModel = new CreateOrEditTestImsModalViewModel(output);


            return PartialView("_CreateOrEditTestImsModal", viewModel);
        }


    }
}